package model

import (
	"context"
	"fmt"
	"github.com/spf13/viper"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"go.mongodb.org/mongo-driver/mongo/readpref"
	"time"
)

// 时间格式化字符串
const SysTimeform string = "2006-01-02 15:04:05"
const SysTimeformShort string = "2006-01-02"

// 中国时区
var SysTimeLocation, _ = time.LoadLocation("Asia/Chongqing")
type DbConf struct {
	Host   string
	User   string
	Pwd    string
	DbName string
}


var MonClient *mongo.Client

func Init()  {
	c := &DbConf{
		Host:   viper.GetString("mongdb.host"),

	}
	url :=fmt.Sprintf("mongodb://%s",c.Host)
	client, err := mongo.NewClient(options.Client().ApplyURI(url))
	if err != nil {
		panic(err)
	}
	ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
	err = client.Connect(ctx)
	if err != nil {
		panic(err)
	}
	errP := client.Ping(ctx, readpref.Primary())
	if errP != nil{
		fmt.Print(errP)
	}

	MonClient = client
}